package com.qianfeng.gl4study.youshare.acitivity.social;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.*;
import android.widget.TextView;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.acitivity.common.LoginActivity;
import com.qianfeng.gl4study.youshare.acitivity.common.RegisterActivity;
import com.qianfeng.gl4study.youshare.adapters.CommonFragmentAdapter;
import com.qianfeng.gl4study.youshare.fragments.GroupCategoryFragment;
import com.qianfeng.gl4study.youshare.fragments.ShareFragment;
import com.qianfeng.gl4study.youshare.fragments.StoreCoreFragment;
import com.qianfeng.gl4study.youshare.fragments.StorePlatformFragment;
import com.qianfeng.gl4study.youshare.model.Category;
import com.qianfeng.gl4study.youshare.view.DataStorage;

import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/3/1
 * Email:pdsfgl@live.com
 */
public class GroupMainActivity extends FragmentActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener{
	private ViewPager storePager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_group_main);

		displayActionBar();

		LinkedList<Fragment> fragments = new LinkedList<Fragment>();

		fragments.add(new ShareFragment());
		// 添加平台产品
		fragments.add(new GroupCategoryFragment());
		CommonFragmentAdapter adapter
				= new CommonFragmentAdapter(
				getSupportFragmentManager(),
				fragments
		);
		storePager.setAdapter(adapter);

		storePager.setOnPageChangeListener(this);
	}

	private void displayActionBar() {

		ActionBar actionBar = getActionBar();

		if (actionBar != null) {

			actionBar.setDisplayHomeAsUpEnabled(true);
			actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));
			actionBar.setDisplayShowCustomEnabled(true);
			LayoutInflater inflater = LayoutInflater.from(this);
			View view = inflater.inflate(R.layout.actionbar_main_title, null);
			TextView txtTitle = (TextView) view.findViewById(R.id.actionbar_title);
			if (txtTitle != null) {
				txtTitle.setText("社交");
				txtTitle.setTextColor(Color.WHITE);
			}
			actionBar.setCustomView(
					view,
					new ActionBar.LayoutParams(
							ViewGroup.LayoutParams.MATCH_PARENT,
							ViewGroup.LayoutParams.WRAP_CONTENT,
							Gravity.CENTER
					)
			);
			actionBar.setDisplayShowTitleEnabled(false);
			actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
			storePager = (ViewPager) findViewById(R.id.social_pager);
			actionBar.setBackgroundDrawable(new ColorDrawable(0xFF000000));
			ActionBar.Tab tab = actionBar.newTab();
			view = inflater.inflate(R.layout.actionbar_main_title, null);
			TextView textActionBar = (TextView) view.findViewById(R.id.actionbar_title);
			textActionBar.setText("分享");
			tab.setCustomView(view);

			tab.setTag("1");
			tab.setTabListener(this);
			actionBar.addTab(tab);

			//// 第二个 Tab
			tab = actionBar.newTab();
			view = inflater.inflate(R.layout.actionbar_main_title, null);
			textActionBar = (TextView) view.findViewById(R.id.actionbar_title);
			textActionBar.setText("圈子");
			tab.setCustomView(view);
			tab.setTag("2");
			tab.setTabListener(this);
			actionBar.addTab(tab);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.store, menu);

		return true;
	}

	/**
	 * 进行登录和注册的跳转
	 * @param item
	 * @return
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// 获取菜单的ID, 就是 <item android:id 这个属性
		int menuId = item.getItemId();

		String msg = "无内容";
		Intent intent = null;
		switch (menuId) {

			case R.id.action_login:     //进行登录操作
				intent = new Intent(this,LoginActivity.class);
				break;
			case R.id.action_register:  //进行组册操作
				intent = new Intent(this,RegisterActivity.class);
				break;
		}

		if(intent!=null){
			startActivity(intent);
			finish();
		}
		return true;
	}

	//////// ActionBar TabListener ///////

	@Override
	public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
		if(storePager != null){
			ActionBar actionBar = getActionBar();
			if(actionBar != null) {
				int index = actionBar.getSelectedNavigationIndex();
				storePager.setCurrentItem(index);
			}
		}
	}

	@Override
	public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

	}

	@Override
	public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

	}

	//////// ViewPager 页面切换进行的事件处理

	@Override
	public void onPageScrolled(int i, float v, int i1) {

	}

	@Override
	public void onPageSelected(int i) {
		ActionBar actionBar = getActionBar();
		if (actionBar != null) {
			actionBar.setSelectedNavigationItem(i);
		}
	}

	@Override
	public void onPageScrollStateChanged(int i) {

	}
}